package com.qf.sms.controller;

import com.qf.base.dto.sms.SmsDTO;
import com.qf.base.result.R;
import com.qf.sms.service.SmsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.sms.controller
 * @Description: 处理短信的controller
 * @Date 2022/8/1 19:04
 */
@RestController
@RequestMapping("qf/sms")
public class SmsController {

    @Resource
    private SmsService smsService;

    @PostMapping("send")
    public R send(@RequestBody SmsDTO smsDTO){
        String phone = smsDTO.getPhone();
        String code = smsDTO.getCode();
        Integer typeId = smsDTO.getTypeId();
        int result = smsService.sendSms(phone, code, typeId);

        if (result > 0){
            //成功
            return R.ok(200,"短信发送成功");
        } else {
            return R.error(500,"短信发送失败");
        }

    }

    @PostMapping("check")
    public R check(@RequestBody SmsDTO smsDTO){
        String phone = smsDTO.getPhone();
        String code = smsDTO.getCode();
        Integer typeId = smsDTO.getTypeId();
        int result = smsService.checkSms(phone, code, typeId);

        if (result == 0){
            return R.ok(200,"短信校验成功");
        } else if (result == 1) {
            return R.error(501,"短信已经过期");
        } else {
            return R.error(502,"短信验证码错误");
        }

    }
}
